import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Welcome from '../components/Welcome.vue'
import Home from '../components/Home.vue'
import User from '../components/User.vue'
import Card from '../components/Card.vue'
import Deal from '../components/Deal.vue'
import Recharge from '../components/Recharge.vue'
import Consume from '../components/Consume.vue'
import Exchange from '../components/Exchange.vue'
import Care from '../components/Care.vue'
import Data from '../components/Data.vue'
//使用路由机制
Vue.use(VueRouter)
const routes = [
  {path: '/', redirect: '/login'},
  {path: '/login', component: Login},
  {path: '/welcome', component: Welcome,redirect: '/home',
    children:[
      {path: '/home', component: Home},
      {path: '/user', component: User},
      {path: '/card', component: Card},
      {path: '/deal', component: Deal},
      {path: '/recharge', component: Recharge},
      {path: '/consume', component: Consume},
      {path: '/exchange', component: Exchange},
      {path: '/care', component: Care},
      {path: '/data', component: Data}
    ]
  }
]

const router = new VueRouter({
  routes
})
//路由导航守卫!!!!!!!
router.beforeEach((to,from,next)=>{
  if(to.path==='/login'){
    return next()
  }
  let token=window.sessionStorage.getItem('token')
  if(token===null||token===''){
    return next("/login")
  }
  next()
})

export default router
